package com.wistech.search.solr.extend;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Required;

import com.wistech.search.solr.extend.model.Product;
import com.wistech.search.solr.extend.service.ProductManager;

public class ProductExternalDataHandler extends
		AbstractExternalDataHandler<Product> implements DocumentHander {
	private ProductManager productManager;

	@Override
	protected void saveData(Product product) {
		productManager.saveProduct(product);
	}

	@Override
	protected void deleteData(Object id) {
		if (id instanceof String) {
			id = Long.parseLong((String) id);
		}
		this.productManager.deleteProduct((Long) id);
	}

	@Override
	protected Product toModel(Map<String, Object> props) {
		Product prod = new Product();
		try {
			BeanUtils.populate(prod, props);
		} catch (IllegalAccessException e) {
		} catch (InvocationTargetException e) {
		}
		return prod;
	}

	/**
	 * @param productManager
	 *            the productManager to set
	 */
	@Required
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

}
